package cn.dlust.tt.module.teacher.service.lessonplans;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.dlust.tt.module.teacher.controller.admin.lessonplans.vo.*;
import cn.dlust.tt.module.teacher.dal.dataobject.lessonplans.LessonPlansDO;
import cn.dlust.tt.framework.common.pojo.PageResult;
import cn.dlust.tt.framework.common.pojo.PageParam;
import cn.dlust.tt.framework.common.util.object.BeanUtils;

import cn.dlust.tt.module.teacher.dal.mysql.lessonplans.LessonPlansMapper;

import static cn.dlust.tt.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.dlust.tt.module.teacher.enums.ErrorCodeConstants.*;

/**
 * 教案 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class LessonPlansServiceImpl implements LessonPlansService {

    @Resource
    private LessonPlansMapper lessonPlansMapper;

    @Override
    public Long createLessonPlans(LessonPlansSaveReqVO createReqVO) {
        // 插入
        LessonPlansDO lessonPlans = BeanUtils.toBean(createReqVO, LessonPlansDO.class);
        lessonPlansMapper.insert(lessonPlans);
        // 返回
        return lessonPlans.getPlanId();
    }

    @Override
    public void updateLessonPlans(LessonPlansSaveReqVO updateReqVO) {
        // 校验存在
        validateLessonPlansExists(updateReqVO.getPlanId());
        // 更新
        LessonPlansDO updateObj = BeanUtils.toBean(updateReqVO, LessonPlansDO.class);
        lessonPlansMapper.updateById(updateObj);
    }

    @Override
    public void deleteLessonPlans(Long id) {
        // 校验存在
        validateLessonPlansExists(id);
        // 删除
        lessonPlansMapper.deleteById(id);
    }

    private void validateLessonPlansExists(Long id) {
        if (lessonPlansMapper.selectById(id) == null) {
            throw exception(LESSON_PLANS_NOT_EXISTS);
        }
    }

    @Override
    public LessonPlansDO getLessonPlans(Long id) {
        return lessonPlansMapper.selectById(id);
    }

    @Override
    public PageResult<LessonPlansDO> getLessonPlansPage(LessonPlansPageReqVO pageReqVO) {
        return lessonPlansMapper.selectPage(pageReqVO);
    }

}